#!/bin/bash

# RocketMQ 环境设置脚本

set -e

echo "🚀 设置 RocketMQ 环境..."

# 检查 Docker 服务状态
echo "📋 检查 Docker 服务状态..."
docker compose ps

echo ""

# 等待服务启动
echo "⏳ 等待服务启动..."
sleep 5

# 创建测试 topic
echo "📝 创建测试 topic..."
docker compose exec broker sh -c "
export NAMESRV_ADDR=nameserver:9876
cd /home/rocketmq/rocketmq-5.1.0/bin
./mqadmin updateTopic -n \$NAMESRV_ADDR -b broker:10911 -t test_topic
"

echo ""

# 检查 topic 列表
echo "📋 检查 topic 列表..."
docker compose exec broker sh -c "
export NAMESRV_ADDR=nameserver:9876
cd /home/rocketmq/rocketmq-5.1.0/bin
./mqadmin topicList -n \$NAMESRV_ADDR
"

echo ""

# 检查 topic 状态
echo "📋 检查 topic 状态..."
docker compose exec broker sh -c "
export NAMESRV_ADDR=nameserver:9876
cd /home/rocketmq/rocketmq-5.1.0/bin
./mqadmin topicStatus -n \$NAMESRV_ADDR -t test_topic
"

echo ""

# 检查 topic 路由信息
echo "📋 检查 topic 路由信息..."
docker compose exec broker sh -c "
export NAMESRV_ADDR=nameserver:9876
cd /home/rocketmq/rocketmq-5.1.0/bin
./mqadmin topicRoute -n \$NAMESRV_ADDR -t test_topic
"

echo ""

# 检查集群信息
echo "📋 检查集群信息..."
docker compose exec broker sh -c "
export NAMESRV_ADDR=nameserver:9876
cd /home/rocketmq/rocketmq-5.1.0/bin
./mqadmin clusterList -n \$NAMESRV_ADDR
"

echo ""

# 检查 broker 状态
echo "📋 检查 broker 状态..."
docker compose exec broker sh -c "
export NAMESRV_ADDR=nameserver:9876
cd /home/rocketmq/rocketmq-5.1.0/bin
./mqadmin brokerStatus -n \$NAMESRV_ADDR -b broker:10911
"

echo ""

echo "✅ RocketMQ 环境设置完成！"
echo ""
echo "🌐 访问地址："
echo "   - 管理控制台: http://localhost:8080"
echo "   - NameServer: localhost:9876"
echo "   - Broker: localhost:10911"
echo ""
echo "💡 如果仍有连接问题，请检查："
echo "   1. 防火墙设置"
echo "   2. 端口是否被占用"
echo "   3. 网络配置" 